package mysql

import "gorm.io/gorm"

type Goods struct {
	gorm.Model
	GoodsTitle  string  `gorm:"column:goods_title;type:varchar(50);not null;comment:商品标题"`
	GoodsPrice  float32 `gorm:"column:goods_price;type:decimal(10,2);not null;comment:商品价格"`
	GoodsStock  int64   `gorm:"column:goods_stock;type:int(11);not null;comment:商品库存"`
	GoodsCover  string  `gorm:"column:goods_cover;type:varchar(100);not null;comment:商品图片"`
	GoodsStatus int64   `gorm:"column:goods_status;type:int(11);not null;comment:商品状态"`
	GoodsType   int64   `gorm:"column:goods_type;type:int(11);not null;comment:商品类型"`
}

// 发布商品
func (g *Goods) CreateGoods() error {
	return mysqlClient.Create(&g).Error
}

// 查询商品ID
func (g *Goods) FindID() error {
	return mysqlClient.Where("id=?", g.ID).First(g).Error
}

// 商品列表
func (g *Goods) List(page int, limit int, goodsTitle string) (goods []*Goods, err error) {
	if goodsTitle != "" {
		mysqlClient.Where("goods_title like ?", "%"+goodsTitle+"%")
	}
	err = mysqlClient.Offset((page - 1) * limit).Limit(limit).Find(&goods).Error
	return
}
